<script setup>
import {
  onMounted,
  getCurrentInstance,
  onUpdated,
  onBeforeUnmount,
  ref,
  watch
} from "vue";
const { appContext } = getCurrentInstance();
const global = appContext.config.globalProperties;

import { get_query_string } from "@/plugins/util";

import {
  websocket_on_message,
  websocket_send,
  websocket_init,
  websocket_reconect
} from "@/plugins/websocket.js";


import {
  websocket_on_message_warning,
  websocket_send_warning,
  websocket_init_warning,
  websocket_reconect_warning,
  websocket_send_new_warning

} from "@/plugins/websocket_warning.js";


/*----语言配置----*/

import { useLangs } from "@/stores/langs.js";
import zhCN from "ant-design-vue/es/locale/zh_CN";
import enUS from "ant-design-vue/es/locale/en_US";
import dayjs from "dayjs";
import "dayjs/locale/zh-cn";
import { useI18n } from "vue-i18n";

dayjs.locale("zh-cn");
let langs = ref("zh");

let useLangs_state = useLangs();
const { t, tm, locale } = useI18n(); //避免使用$t。

/*全局socket - 定时任务*/
let timer_check_status = null; //10s钟点发起重连检查。
let timer_outline = null; //30s钟假定socket离线。
let socket_status = null;


/*全局socket - 定时任务---告警*/
let timer_check_status_warning = null; //10s钟点发起重连检查。
let timer_outline_warning = null; //30s钟假定socket离线。
let socket_status_warning = null;

onUpdated(() => {
  console.log("app-****-updated");
});

function is_reconnect() {

  socket_status = window.sessionStorage.getItem('socket_status');

  if (window.my_console == 1) {
    console.log("-socket_status--" + socket_status);
  }


  if (socket_status == "false" || socket_status == null) {
    console.log(`%c 重连检测-重连-socket_status--${socket_status}`, "color:red");
    websocket_reconect();
  } else {
    console.log(`%c 重连检测-不重连-socket_status--${socket_status}`, "color:green");
  }
}

function is_reconnect_warning() {

  socket_status_warning = window.sessionStorage.getItem('socket_status_warning');

  if (window.my_console == 1) {
    console.log("-socket_status_warning--" + socket_status_warning);
  }


  if (socket_status == "false" || socket_status == null) {
    console.log(`%c 重连检测-重连-socket_status_warning--${socket_status}`, "color:red");
    websocket_reconect_warning();
  } else {
    console.log(`%c 重连检测-不重连-socket_status_warning--${socket_status}`, "color:green");
  }
}

function getin_ful() {
  // 获取要全屏的元素
  var element = document.documentElement;

  if (element.requestFullscreen) {
    element.requestFullscreen();
  } else if (element.mozRequestFullScreen) { // Firefox
    element.mozRequestFullScreen();
  } else if (element.webkitRequestFullscreen) { // Chrome, Safari and Opera
    element.webkitRequestFullscreen();
  } else if (element.msRequestFullscreen) { // IE/Edge
    element.msRequestFullscreen();
  }

}


watch(
  () => useLangs_state.cur_lang_getter,
  (val) => {
    langs.value = val;
    if (val == "zh") dayjs.locale("zh-cn");
    else dayjs.locale("en");
  },
  { deep: true }
);


onMounted(() => {

  getin_ful();

  console.log("app-onMounted");

  /*--刷新后-语言保持---*/
  langs.value = window.sessionStorage.getItem("langs") || "zh";
  if (langs.value == "zh") dayjs.locale("zh-cn");
  else dayjs.locale("en");

  //默认语言环境 非组件
  locale.value = "zh";
  window.sessionStorage.setItem("langs", "zh");
  useLangs_state.ch_cur_lang("zh");
  // cur_lang.value = "zh";
  /*--语言保持---*/


  // is_reconnect();
  //10s 检查socket状态
  timer_check_status = setInterval(() => {
    // is_reconnect();
  }, 10 * 1000)

  //30s假定socket离线
  timer_outline = setInterval(() => {
    window.sessionStorage.setItem('socket_status', "false");
  }, 20 * 1000)



  // is_reconnect_warning();
  //10s 检查socket状态
  timer_check_status_warning = setInterval(() => {
    // is_reconnect_warning();
  }, 10 * 1000)

  //30s假定socket离线
  timer_outline_warning = setInterval(() => {
    window.sessionStorage.setItem('socket_status_warning', "false");
  }, 20 * 1000);

  setTimeout(() => {
    // websocket_send_new_warning({});
  }, 5 * 1000)



});

onBeforeUnmount(() => {

  // timer_check_status && clearInterval(timer_check_status);
  // timer_outline && clearInterval(timer_outline);


  // timer_check_status_warning && clearInterval(timer_check_status_warning);
  // timer_outline_warning && clearInterval(timer_outline_warning);


});


</script>

<template>
  <a-config-provider :locale="langs === 'en' ? enUS : zhCN">
    <RouterView />
  </a-config-provider>
</template>

<style scoped>
header {
  line-height: 1.5;
  max-height: 100vh;
}

.logo {
  display: block;
  margin: 0 auto 2rem;
}

nav {
  width: 100%;
  font-size: 12px;
  text-align: center;
  margin-top: 2rem;
}

nav a.router-link-exact-active {
  color: var(--color-text);
}

nav a.router-link-exact-active:hover {
  background-color: transparent;
}

nav a {
  display: inline-block;
  padding: 0 1rem;
  border-left: 1px solid var(--color-border);
}

nav a:first-of-type {
  border: 0;
}

@media (min-width: 1024px) {
  header {
    display: flex;
    place-items: center;
    padding-right: calc(var(--section-gap) / 2);
  }

  .logo {
    margin: 0 2rem 0 0;
  }

  header .wrapper {
    display: flex;
    place-items: flex-start;
    flex-wrap: wrap;
  }

  nav {
    text-align: left;
    margin-left: -1rem;
    font-size: 1rem;

    padding: 1rem 0;
    margin-top: 1rem;
  }
}
</style>


<style>
.ant-modal-content {
  background-color: rgb(26, 53, 106) !important;
  box-shadow: unset !important;
}

.ant-modal-header {
  background-color: unset !important;
  background-color: rgb(26, 53, 106) !important;
}

.ant-modal-title {
  color: #fff !important;
  ;
}

.anticon.anticon-close {
  color: #fff !important;
  ;
}

.ant-modal-confirm-title {
  color: #fff !important;
}

.ant-modal-confirm-content {
  color: #fff !important;
}

.ant-form-item .ant-form-item-label >label{
  color:#FFF!important;
}
</style>